package com.open.capacity.notice.controller;

import javax.validation.Valid;

import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.open.capacity.common.dto.ResponseEntity;
import com.open.capacity.common.validator.UpdateGroup;
import com.open.capacity.notice.dmo.SendMessageJob;
import com.open.capacity.notice.dmo.SendMessageTask;
import com.open.capacity.notice.dto.SendMessageJobDto;
import com.open.capacity.notice.manager.SendMessageManager;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 前端控制器
 * </p>
 * 
 * @since 2024-05-06
 */
@RestController
@SuppressWarnings("all")
@RequiredArgsConstructor
@RequestMapping("/sendMessageJob")
public class SendMessageJobController {

	private final SendMessageManager sendMessageManager;

	/**
	 * 分页列表查询
	 * 
	 * @param sendMessageJob
	 * @param page
	 * @return
	 */
	@ApiOperation(value = "消息任务管理-分页列表查询", notes = "消息任务管理-分页列表查询")
	@GetMapping(value = "/list")
	public ResponseEntity queryPageList(SendMessageJob sendMessageJob, Page page) {
		return sendMessageManager.queryPageList(sendMessageJob, page);
	}

	/**
	 * 添加
	 *
	 * @param sendMessageJobDto
	 * @return
	 */
	@PostMapping(value = "/add")
	@ApiOperation(value = "消息任务管理-添加", notes = "消息任务管理-添加")
	public ResponseEntity<SendMessageJob> add(@Valid @RequestBody SendMessageJobDto sendMessageJobDto) {
		SendMessageJob sendMessageJob = sendMessageManager.add(sendMessageJobDto);
		return ResponseEntity.succeed(sendMessageJob, "添加成功");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "消息任务管理-通过id查询", notes = "消息任务管理-通过id查询")
	@GetMapping(value = "/query-by-id")
	public ResponseEntity<SendMessageJob> queryById(@RequestParam(name = "id") String id) {
		return sendMessageManager.queryById(id);
	}

	/**
	 * 分页列表查询
	 * 
	 * @param sendMessageTask
	 * @param page
	 * @return
	 */
	@ApiOperation(value = "消息子任务管理-分页列表查询", notes = "消息子任务管理-分页列表查询")
	@GetMapping(value = "/list/{jobId}")
	public ResponseEntity queryTaskPageList(@PathVariable(name = "jobId") String jobId, SendMessageTask sendMessageTask, Page page) {
		sendMessageTask.setJobId(jobId);
		return sendMessageManager.queryTaskPageList(sendMessageTask, page);
	}

	/**
	 * 编辑
	 *
	 * @param sendMessageJobDto
	 * @return
	 */
	@ApiOperation(value = "消息任务管理-编辑", notes = "消息任务管理-编辑")
	@PostMapping(value = "/edit")
	public ResponseEntity<Boolean> edit(@Validated({ UpdateGroup.class }) @RequestBody SendMessageJobDto sendMessageJobDto) {
		return sendMessageManager.edit(sendMessageJobDto);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "消息任务管理-通过id删除", notes = "消息任务管理-通过id删除")
	@PostMapping(value = "/delete")
	public ResponseEntity<Boolean> delete(@RequestParam(name = "id") String id) {
		return sendMessageManager.delete(id);
	}

	/**
	 * 重新分解
	 * 
	 * @param sendMessageJobDto
	 * @return
	 */
	@ApiOperation(value = "消息任务管理-重新分解", notes = "消息任务管理-重新分解")
	@PostMapping(value = "/resplit")
	public ResponseEntity resend(@RequestBody SendMessageJobDto sendMessageJobDto) {
		SendMessageJob src = sendMessageManager.queryById(sendMessageJobDto.getId()).getData();
		Assert.isTrue(src.isCreated(), "只有未分解过子任务的才允许重新分解");
		SendMessageJob sendMessageJob = sendMessageManager.add(sendMessageJobDto);
		return ResponseEntity.succeed(sendMessageJob, "重新分解成功");
	}

}
